const inquirer = require("inquirer");
const path = require("path");
const fs = require("fs");
const prettier = require("prettier");
const { log, stringCase, cpFile, cpDir } = require("./utils");
const t = require("../config/dict");
const { boolChoies, microAppType } = require("../config");

module.exports = (context) => {
  const destPath = path.resolve(process.cwd(), context.metadata.projectDir),
    localPath = path.resolve(
      __dirname,
      "../baseTemplate/qiankun/modules/" +
        context.metadata.projectTechnologyStack +
        "/" +
        (context.metadata.projectDevelopLang === "javascript"
          ? "default"
          : "default-ts")
    ),
    fileExt =
      context.metadata.projectDevelopLang === "javascript" ? ".js" : ".ts";
  if (context.metadata.projectType[0].indexOf("No UI") > -1) {
    return inquirer
      .prompt([
        {
          name: "projectQiankun",
          message: t(context.lang, "适配微前端") + "?",
          default: "yes",
          pageSize: 3,
          type: "rawlist",
          choices: [...t(context.lang, boolChoies)],
        },
      ])
      .then((answers) => {
        let bool = answers.projectQiankun;
        context.metadata.projectQiankun = answers.projectQiankun;
        if (bool === "yes") {
          let package = require(destPath + "/package.json"),
            tempPackage = require(localPath + "/package.json");

          package.dependencies["qiankun"] = tempPackage.dependencies.qiankun;
          fs.writeFileSync(
            destPath + "/package.json",
            prettier.format(JSON.stringify(package), {
              semi: false,
              parser: "json",
            })
          );

          log("success", {
            message: [
              stringCase(t(context.lang, "更新")),
              "package.json",
              ",",
              t(context.lang, "完成"),
              t(context.lang, "。"),
            ].join(" "),
          });

          const cpBaseFile = () => {
              return cpFile(
                localPath + "/public-path.js",
                destPath + "/public-path.js"
              );
            },
            cpBaseConfig = () => {
              return cpFile(
                localPath + "/vue.config.js",
                destPath + "/vue.config.js"
              );
            },
            buildQiankunBase = () => {
              return cpDir(localPath + "/microApp", destPath + "/src/microApp");
            };

          return cpBaseFile()
            .then(() => {
              return cpBaseConfig();
            })
            .then(() => {
              return buildQiankunBase();
            })
            .then(() => {
              log("success", {
                message: [
                  stringCase(t(context.lang, "更新")),
                  t(context.lang, "基础配置"),
                  ",",
                  t(context.lang, "完成"),
                  t(context.lang, "。"),
                ].join(" "),
              });
              return inquirer
                .prompt([
                  {
                    name: "projectQiankunType",
                    message: t(context.lang, "微前端类型") + "?",
                    default: "hostApp",
                    pageSize: 3,
                    type: "rawlist",
                    choices: [...t(context.lang, microAppType)],
                  },
                ])
                .then((answers) => {
                  context.metadata.projectQiankunType =
                    answers.projectQiankunType;
                  let baseName = "host-app-main" + fileExt,
                    routerName = "host-app-index" + fileExt;
                  if (answers.projectQiankunType !== "hostApp")
                    (baseName = "sub-app-main" + fileExt),
                      (routerName = "sub-app-index" + fileExt);
                  return cpFile(
                    localPath + "/router/" + routerName,
                    destPath + "/src/router/index" + fileExt
                  ).then(() => {
                    return cpFile(
                      localPath + "/" + baseName,
                      destPath + "/src/main" + fileExt
                    );
                  });
                });
            })
            .then(() => {
              log("success", {
                message: [
                  stringCase(t(context.lang, "适配微前端")),
                  t(context.lang, "完成"),
                  t(context.lang, "。"),
                ].join(" "),
              });
              return context;
            });
        } else {
          return context;
        }
      });
  } else {
    return context;
  }
};
